//
//  YFWelcomePageController.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/17.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFWelcomePageController.h"
#import "YFWelcomeDeviceVC.h"
#import "YFUpheadIocnVC.h"
#import "YFRacquetController.h"
#import "YFPersonDataVC.h"
#import "YFEquipmentCalibrationVC.h"
#import "YFMMDrawerStateManager.h"
#import "JCTabBarViewController.h"

@interface YFWelcomePageController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>

@property (nonatomic, strong) UIPageViewController *pageController;
@property (nonatomic, strong) NSArray *pageViewControllers;
@property (nonatomic, strong) UserInfoItem *cacheUserInfoItem;

/** 跳过Item */
@property (nonatomic, strong) UIButton *skipBarButton;

@end

@implementation YFWelcomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.yfBaseDelegate = self;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.cacheUserInfoItem = [[UserInfoItem alloc] init];
    [[User getUseInfo] copyValuesTo:self.cacheUserInfoItem];
    
    [self creatViewComtrollers];
    [self addChildViewController:self.pageController];
    [self.view addSubview:self.pageController.view];
    [self.pageController didMoveToParentViewController:self];
    
    UIButton *skipButton = [UIButton creatButtonTitle:@"Skip" bgColor:nil textFont:kOpenSansFont(OpenSansBoldItalic, 16) textColor:kYellowGreenColor];
    self.skipBarButton = skipButton;
    [skipButton addTarget:self action:@selector(skipBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [skipButton sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:skipButton];
}

- (void)skipBarButtonAction:(UIBarButtonItem *)skipItem {
    AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    appdelegate.window.rootViewController = [[YFMMDrawerStateManager sharedManager] FetchDrawerController];
    appdelegate.window.rootViewController = [JCTabBarViewController new];
}

- (void)customBaseBackNavBarItemAction:(UIBarButtonItem *)backItem {
    // 上一页
    [self nextOrPrevious:NO];
}

// 上一页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSInteger index = [self indexOfViewController:viewController];
    if ((index == 0) || (index == NSNotFound)) {
        NSLog(@"page index is equal to first Page Number or index not found");
        return nil;
    }else {
        index--;
        return [self viewControllerAtIndex:index];
    }
}

// 下一页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSInteger index = [self indexOfViewController:viewController];
    if (index == NSNotFound || index >= 3) {
        return nil;
    }else {
        index++;
        return [self viewControllerAtIndex:index];
    }
}

- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED {
    return UIInterfaceOrientationPortrait;
}


- (UIPageViewController *)pageController {
    if (!_pageController) {
        // 设置UIPageViewController的配置项
        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationNone] forKey:UIPageViewControllerOptionSpineLocationKey];
        
        _pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
        //        _pageController.dataSource = self; // 打开就可以滑动
        _pageController.delegate = self;
        _pageController.view.frame = self.view.bounds;
        
        UIViewController *initialViewController =[self viewControllerAtIndex:0];
        
        NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
        
        [_pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    }
    return _pageController;
}

#pragma mark - 手动控制上一页下一页
- (void)nextOrPrevious:(BOOL)next {
    NSInteger index = [self indexOfViewController:self.pageController.viewControllers.lastObject];
    // 当蓝牙已经连接上，怎直接跳过Device 控制器
    
    if (next){
        // 进到下一页
        if (index < self.pageViewControllers.count - 1) {
            index++;
            if (index == 3 && [YFCommunicationManager shareInstance].isPeripheralConnected) {
                index++;
            }
            [self.pageController setViewControllers:@[[self viewControllerAtIndex:index]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
            }];
        }
    }else {
        // 返回上一页
        if (index > 0) {
            index--;
            if (index == 3 && [YFCommunicationManager shareInstance].isPeripheralConnected) {
                index--;
            }
            [self.pageController setViewControllers:@[[self viewControllerAtIndex:index]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL finished) {
                
            }];
        }
    }
    self.skipBarButton.hidden = YES;
    if (index == 0) {
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }else {
        if (index == 1) {
            self.title = @"Personal Data";
        }else if (index == 2){
            self.title = @"Racquet";
        }else if (index == 3){
            self.skipBarButton.hidden = NO;
            self.title = @"Connection Devices";
        }else if (index == 4) {
            self.skipBarButton.hidden = NO;
            self.title = @"Equipment Calibration";
        }
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
}

- (void)creatViewComtrollers {
    __weak typeof(self) weakSelf = self;
    
    YFUpheadIocnVC *uploadIconVc = [[YFUpheadIocnVC alloc] initWithNibName:@"YFUpheadIocnVC" bundle:nil];
    uploadIconVc.cacheUserInfoItem = self.cacheUserInfoItem;
    [uploadIconVc setNextButtonClick:^{
        [weakSelf nextOrPrevious:YES];
    }];
    
    YFPersonDataVC *personVc = [[YFPersonDataVC alloc] init];
    personVc.isWecomePersonVc = YES;
    personVc.cacheUserInfoItem = self.cacheUserInfoItem;
    [personVc setNextButtonClick:^{
        [weakSelf nextOrPrevious:YES];
    }];
    
    YFRacquetController *racquetVc = [[YFRacquetController alloc] init];
    racquetVc.isWecomeRacquetVc = YES;
    racquetVc.cacheUserInfoItem = self.cacheUserInfoItem;
    [racquetVc setNextButtonClick:^{
        [weakSelf nextOrPrevious:YES];
    }];
    
    YFWelcomeDeviceVC *deviceVc = [[YFWelcomeDeviceVC alloc] initWithNibName:@"YFWelcomeDeviceVC" bundle:nil];
    [deviceVc setSkipOrNextStepBlock:^(BOOL isNextStep) {
        // 进入到设备校准页面
        [weakSelf nextOrPrevious:YES];
//        if (isNextStep){
//
//        }else {
//            [UIApplication sharedApplication].keyWindow.rootViewController = [[YFMMDrawerStateManager sharedManager] FetchDrawerController];
//        }
    }];
    
    YFEquipmentCalibrationVC *equipmentVc = [[YFEquipmentCalibrationVC alloc] initWithNibName:NSStringFromClass([YFEquipmentCalibrationVC class]) bundle:nil];
    equipmentVc.isWecomeEquipmentVc = YES;
    [equipmentVc setSkipOrNextStepBlock:^(BOOL isNextStep) {
        if (isNextStep == NO) {
            AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//            appdelegate.window.rootViewController = [[YFMMDrawerStateManager sharedManager] FetchDrawerController];
            appdelegate.window.rootViewController = [JCTabBarViewController new];
        }
    }];
    self.pageViewControllers = @[uploadIconVc,personVc,racquetVc,deviceVc,equipmentVc];
}

// 得到相应的VC对象
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (self.pageViewControllers.count == 0 || index >= self.pageViewControllers.count) {
        return nil;
    }
    return [self.pageViewControllers objectAtIndex:index];
}
// 根据数组元素值，得到下标值
- (NSUInteger)indexOfViewController:(UIViewController *)viewControlller {
    return [self.pageViewControllers indexOfObject:viewControlller];
}

@end
